﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace BMSPlatform.Control
{
    public class CustomPanel : StackPanel
    {
        public Brush LineBrush
        {
            get { return (Brush)GetValue(LineBrushProperty); }
            set { SetValue(LineBrushProperty, value); }
        }

        public static readonly DependencyProperty LineBrushProperty =
        DependencyProperty.Register("LineBrush", typeof(Brush), typeof(CustomPanel), new PropertyMetadata(null));

        public double LineThiness
        {
            get { return (double)GetValue(LineThinessProperty); }
            set { SetValue(LineThinessProperty, value); }
        }

        public static readonly DependencyProperty LineThinessProperty = DependencyProperty.Register("LineThiness", typeof(double), typeof(CustomPanel), new PropertyMetadata(0d, new PropertyChangedCallback((o, e) => { (o as Panel).InvalidateVisual(); })));

        public double Offset
        {
            get { return (double)GetValue(OffsetProperty); }
            set { SetValue(OffsetProperty, value); }
        }
        public static readonly DependencyProperty OffsetProperty = DependencyProperty.Register("Offset", typeof(double), typeof(CustomPanel), new PropertyMetadata(0d, new PropertyChangedCallback((o, e) => { (o as Panel).InvalidateVisual(); })));

        protected override Size ArrangeOverride(Size arrangeSize)
        {
            return base.ArrangeOverride(arrangeSize);
        }

        protected override Size MeasureOverride(Size constraint)
        {
            return base.MeasureOverride(constraint);
        }

        protected override void OnRender(DrawingContext dc)
        {
            base.OnRender(dc);
            if (Children.Count > 0)
            {
                TreeViewItem child;
                Point point;
                Pen pen = new Pen(LineBrush, LineThiness);
                Point startPoint = new Point(Offset, 0), endPoint = new Point(Offset, 0);
                for (int i = 0; i < Children.Count - 1; i++)
                {
                    child = Children[i] as TreeViewItem;
                    point = child.TranslatePoint(new Point(), this);
                    startPoint.Y = point.Y;
                    endPoint.Y = point.Y + child.RenderSize.Height;
                    dc.DrawLine(pen, startPoint, endPoint);
                }
                child = Children[Children.Count - 1] as TreeViewItem;
                point = child.TranslatePoint(new Point(), this);
                startPoint.Y = point.Y;
                if (!child.IsExpanded || child.Items == null || child.Items.Count < 1)
                    endPoint.Y = point.Y + child.RenderSize.Height / 2;
                else
                    endPoint.Y = point.Y + child.RenderSize.Height / child.Items.Count / 2;
                dc.DrawLine(pen, startPoint, endPoint);
            }
        }
    }
}
